package hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import hospital.common.utils.MD5;
import hospital.exception.HospitalException;
import hospital.model.hosp.HospitalSet;
import hospital.result.Result;
import hospital.service.HospitalSetService;
import hospital.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author
 * @description:
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin //允许跨域访问
public class HospitalSetController {
    @Autowired
    HospitalSetService hospitalSetService;

    /**
     * @Author yang
     * @Description //TODO 查询所有的医院摄者管理
     * @Param []
     * @return
     **/
    @ApiOperation("查询所有的医院摄者管理")
    @GetMapping("/findAll")
    public Result findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * @Author yang
     * @Description //TODO 逻辑删除指定id的医院设置
     * @Param [id]
     * @return
     **/
    @ApiOperation("删除指定id的医院设置")
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id){
        boolean b = hospitalSetService.removeById(id);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * @Author yang
     * @Description //TODO 得到医院的设置
     * @Param
     * @return
     **/
    //5 根据id获取医院设置
    @GetMapping("/getHospitalSetInfo/{id}")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }
    /**
     * @Author yang
     * @Description //TODO 多条件分页查询
     * @Param
     * @return
     **/
    @ApiOperation("多条件分页查询")
    @PostMapping("/find/{current}/{limit}")
    public Result queryMoreCondition(@PathVariable("current") long current,
                                     @PathVariable("limit") long limit,
                                     //传入的参数为JSON格式 required= false不为空
                                     @RequestBody(required= false) HospitalSetQueryVo vo){
        //创建分页对象
        Page<HospitalSet> page=new Page<>();
        page.setCurrent(current);//当前页
        page.setSize(limit);//每页的条数

        //构建查询条件
        QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
        String hosname = vo.getHosname(); //医院名称
        String hoscode = vo.getHoscode(); //医院编号
        if(!StringUtils.isNullOrEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if(!StringUtils.isNullOrEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
    }

    /**
     * @Author yang
     * @Description //TODO 添加医院设置
     * @Param
     * @return
     **/
    @PostMapping("/saveHospital")
    public Result saveHospital(@RequestBody HospitalSet set ){
        //设置当前医院的状态 0使用，1 不能使用
        set.setStatus(0);
        //设置签名秘钥
        Random random=new Random();
        set.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(10)));

        boolean save = hospitalSetService.save(set);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * @Author yang
     * @Description //TODO 修改医院的设置
     * @Param
     * @return
     **/
    @PostMapping("/updateHospital")
    public Result updateHospital(@RequestBody HospitalSet set){
        boolean b = hospitalSetService.updateById(set);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * @Author yang
     * @Description //TODO 批量删除
     * @Param
     * @return
     **/
    @DeleteMapping("/deleteHospitalIds")
    public Result deleteHospitalIds(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * @Author yang
     * @Description //TODO 锁定医院设置的签名
     * @Param
     * @return
     **/
    @PutMapping("/lockHospital/{id}/{status}")
    public Result lockHospital(@PathVariable long id,
                               @PathVariable int status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }
    /**
     * @Author yang
     * @Description //TODO 发送签名
     * @Param
     * @return
     **/
    @GetMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String signKey = hospitalSet.getSignKey();

        return Result.ok();
    }
}


